function t(t) {
    return (t = t.toString())[1] ? t : "0" + t;
}

var e = function(t) {
    if ("" == t) return t;
    if (-1 == t.indexOf(":")) {
        var e = t.substring(0, 2);
        return e.startsWith("0") && (e = e.substring(1)), e + ":" + t.substring(2);
    }
    return t.startsWith("0") ? t.substring(1) : t;
}, a = function(t, a) {
    var r = e(t)?e(t):" --", n = e(a)?e(a):" --", s = "";
    return r && (s = "首" + r), n && (s = s + " 末" + n), s;
}, 
	 arriveText = function(e,t) { /** 通到站文案 **/
	     var station_left = parseInt(t), arrival = parseInt(e.arrival), dis = parseInt(e.dis);
	     if (station_left < 0) return  {text:"等待首站发车",time:"",dis:"",status:2,line_name:e.line_name,item:e};;
	     if (0==station_left && dis<=90) {
	         return {text:"车已到达",time:"",dis:"",status:2,line_name:e.line_name,item:e};
	     } else if(arrival > 3600) {
	 		return {text:"等待超过一个小时",time:"",dis:"",status:2,line_name:e.line_name,item:e};
	     }else if( 0==station_left&&dis<=200){
	 		 return {text:"即将到站",time:Math.round(arrival / 60)==0?1:Math.round(arrival / 60),dis:dis + '米',status:1,line_name:e.line_name,item:e}
	 	} else {
	 		 return (station_left==0) ? {text:"下站到达",time: Math.round(arrival / 60)==0?1:Math.round(arrival / 60),dis:(e.dirt>1 ? e.dirt+"公里" : dis + '米'),status:1,line_name:e.line_name,item:e}:{text:station_left+1 + "站",time: Math.round(arrival / 60),dis:(e.dirt>1 ? e.dirt+"公里" : dis + '米'),status:1,line_name:e.line_name,item:e};
	     }
	 },
    prettifySeconds = function(t) {
		 return t >= 60 ? {
			 value: Math.round(t / 60),
			 unit: "分钟"
		 } : t > 30 ? {
			 value: 1,
			 unit: "分钟"
		 } : {
			 value: 30,
			 unit: "秒"
		 };
	 },
	 prettifyTimeStamp = function(t) {
		 var e = new Date(t);
		 return "".concat(e.getHours(), ":").concat(e.getMinutes() > 9 ? e.getMinutes() : "0" + e.getMinutes());
	 },
	 prettifyMeters = function(e) {
		 var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "cn";
		 if (e <= 0) return {};
		 if (e < 100) return {
			 value: "小于100",
			 unit: "en" === t ? "m" : "米"
		 };
		 if (e < 1e3) return {
			 value: e,
			 unit: "en" === t ? "m" : "米"
		 };
		 var r = Math.ceil(e / 100), u = r % 10;
		 
		 return {
			 value: (r / 10).toFixed(0 === u ? 0 : 1),
			 unit: "en" === t ? "km" : "千米"
		 };
	 },
	 arriveTextNew = function(item,index) { /** 通到站文案 **/
	 
	    var orderDistance = index - item.order ;
		var travel = item.travels[0]
		var text = "";
		var arrivalTime = "";
		var distance = ""
		var travelTime = ""
		// 到下一站状态
		var toNextStatus = ""
		if(item.distanceToSc < 60){
			toNextStatus = "已到站"
		} else{
			toNextStatus = "即将到站"
		}
		if(orderDistance>=0){
			if(orderDistance == 0){
				text =  item.state == 1 ? "已到站" : "即将到站"
			} else {
				text = orderDistance + "站"
			}			
			
			if(travel.arrivalTime){
				arrivalTime = travel.arrivalTime ? prettifyTimeStamp(travel.arrivalTime) : ""
			}
			if(travel.travelTime){
				travelTime = travel.travelTime ? prettifySeconds(travel.travelTime) : ""
			}
			distance = prettifyMeters(item.distanceToWaitStn)
		}		
		
	
	   return {
		   toNextStatus: toNextStatus,
		   text: text,
		   arrivalTime: arrivalTime,
		   distance: distance,
		   travelTime: travelTime,
	   }
		 
	 },

mapArriveText = function(e,t,m) { /** 通到站文案 **/
    var station_left = parseInt(t), arrival = parseInt(e.arrival), dis = parseInt(e.dis);
    if (0==station_left && dis<=90) {
        return {text:"车已到达"+m,item:e};
    } else if(arrival > 3600) {
		return {text:"等待超过一个小时",item:e};
    }else if( 0==station_left&&dis<=200){
		 return {text:"即将到达"+m,item:e}
	} else {
		 return  {text:(e.dirt>1 ? e.dirt+"公里" : dis + '米')+'后抵达'+m,item:e}	
    }
};


module.exports = {
    formatBusTime: a,
    parseNomalBus: function(t) {
        var e = {};
        return e.frontName = t.from, e.terminalName = t.to, e.directInverseId = t.reveser_id, 
        e.bustime = a(t.begin_time, t.end_time),
        e.length = t.distance ? "全程:" + parseFloat(t.distance).toFixed(1) + "公里" : "全程: --公里",
		e.price = t.price?"票价:"+t.price+"元":"票价 --",
        e.stations = t.stations, e.loop = t.loop, e;
    },
    calculateDistance: function(t, e) {
        var a = t.lng, r = t.lat, n = e.lng, s = e.lat;
        a *= .01745329251994329, r *= .01745329251994329, n *= .01745329251994329, s *= .01745329251994329;
        var i = Math.sin(a), u = Math.sin(r), o = Math.cos(a), c = Math.cos(r), l = Math.sin(n), f = Math.sin(s), h = Math.cos(n), m = Math.cos(s), v = [], p = [];
        v[0] = c * o, v[1] = c * i, v[2] = u, p[0] = m * h, p[1] = m * l, p[2] = f;
        var d = Math.sqrt((v[0] - p[0]) * (v[0] - p[0]) + (v[1] - p[1]) * (v[1] - p[1]) + (v[2] - p[2]) * (v[2] - p[2])), g = 12742001.5798544 * Math.asin(d / 2);
        return parseFloat(g.toFixed(3));
    },
	/** 解析实时公交站点 **/
	 parseRealtimeMapText: function(busData,selectStation) {
	     var trip = busData.trip, data = {trip:[]}, select_station_num = selectStation.station_num,select_name = selectStation.name;
	//      if ( busData.status == 1) {
				
				// console.log(trip)
				trip.forEach(function(item){
						data.trip.push(
							mapArriveText(item,item.station_left,select_name)
						)
				})
	     return data;
	 },
	 prettifySeconds: function (t) {
	     return t >= 60
	       ? { value: Math.round(t / 60), unit: "分钟" }
	       : t > 30
	       ? { value: 1, unit: "分钟" }
	       : { value: 30, unit: "秒" };
	   },
	   
	 formatebus: function (e) {
	     var t = {
	       emTime: { time: "--", unit: "", rType: "" },
	       secondTime: {},
	       thirdTime: {},
	     };
	     return (
	       e.forEach(function (n, i) {
	         var a;
	         switch (n.value) {
	           case 0:
	             a = function () {
	               this.time = "已到站";
	             };
	             break;
	           default:
	             a =
	               n.travelTime >= 3600
	                 ? function () {
	                     this.time = u(n.arrivalTime);
	                   }
	                 : s(n.travelTime);
	         }
	         switch (i) {
	           case 0:
	             e.length < 2 && (t.secondTime.time = "下一班时间"),
	               a.call(t.emTime),
	               (t.emTime.rType =
	                 n.rType !== 1 &&
	                 "已到站" !== t.emTime.time);
	             break;
	           case 1:
	             a.call(t.secondTime);
	             break;
	           case 2:
	             a.call(t.thirdTime);
	         }
	       }),
	       t
	     );
	   },
	   
	calRotate: function(lat1, lon1, lat2, lon2) {
	    // return 180 * Math.atan2(lng2 - lng1, lat2 - lat2) / Math.PI - 90;
		if(lat1&&lon1&&lat2,lon2){
			const dLon = ((lon2 - lon1) * Math.PI) / 180;
			const y = Math.sin(dLon) * Math.cos((lat2 * Math.PI) / 180);
			const x =
			  Math.cos((lat1 * Math.PI) / 180) * Math.sin((lat2 * Math.PI) / 180) -
			  Math.sin((lat1 * Math.PI) / 180) *
				Math.cos((lat2 * Math.PI) / 180) *
				Math.cos(dLon);
			const brng = (Math.atan2(y, x) * 180) / Math.PI;
			const angle = (brng + 360) % 360;
			return angle;
		} else {
			return 0;
		}
	
		

	},
	 

    /** 解析实时公交站点 **/
    parseRealtimeBusLineStation: function(line,index) {
        var buses = line.buses;
		var data = {};


		switch (line.state) {
		   case 0:
		 	data.status = 0
		 	data.notice = '有实时车辆' 
		 	break;
		   case -1:
		 	  data.status = -1
		 	  data.notice = '等待发车' 
		 	break;
		   case -2:
		 	  data.status = -2
		 	  data.notice = '暂无信息' 
		 	break;
		   case -3:
		 	  data.status = -3
		 	  data.notice = '不在运营时间' 
		 	break;
		   case -4:
		 	data.status = -4
		 	data.notice = '首班未发' 
		 	break;
		   case -5:
		 	  data.status = -5
		 	  data.notice = '尚未开通'
		 	break;
		   case 1:
		 	  data.status = 1
		 	  data.notice = '线路已变更'
		 	break;
		   case 2:
		 	  data.status = 2
		 	  data.notice = '站点已变更'
		 }

		 
		 
		 if(buses.length){
			 buses.forEach(function(item,indexa){
				 var res = arriveTextNew(item,index)
				 buses[indexa]['text'] = res.text
				 buses[indexa]['arrivalTime'] = res.arrivalTime
				 buses[indexa]['travelTime'] = res.travelTime
				 buses[indexa]['distance'] = res.distance
				 buses[indexa]['toNextStatus'] = res.toNextStatus
				 
				 // data.buses.push({
					 
				 // })
			 })
		 } 
		 data.buses= buses
		 // console.log('data.buses',data)
		return data;
    },
    formaDatetTime: function(e) {
        return [ e.getHours(), e.getMinutes() ].map(t).join(":");
    },
   getDistance: function(t, e) {
		function n(t) {
		            return t * i / 180;
		        }
		var i = Math.PI, o = n(t.latitude), u = n(e.latitude), a = o - u, r = n(t.longitude) - n(e.longitude), c = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(o) * Math.cos(u) * Math.pow(Math.sin(r / 2), 2)));
		return c *= 6378137, c = Math.round(1e4 * c) / 1e4;
	},
	getDistanceNew: function(t)  {
		function Pix(t) {
		    return t * Math.PI / 180;
		}
	    var lat1 = t.lat1, 
		    lng1 = t.lng1,
			lat2 = t.lat2, 
			lng2 = t.lng2, 
			// pilat1 = Pix(lat1), 
			// pilat2 = Pix(lat2), 
			sublat = Pix(lat1) - Pix(lat2), 
			sublng = Pix(lng1) - Pix(lng2), 
			res = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(sublat / 2), 2) + Math.cos(Pix(lat1)) * Math.cos(Pix(lat2)) * Math.pow(Math.sin(sublng / 2), 2)));
	    return res *= 6378.137, res = Math.round(1e3 * res);
	},
    calculateBusRealTime: arriveText,
    checkadcode: function(t) {
        var e = !0;
        switch (t.substring(0, 4)) {
          case "1100":
          case "1101":
          case "3301":
          case "3100":
          case "3101":
            e = !1;
        }
        return e;
    }
};